package cn.wlh.learn.webmagic.selenium.discuz;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HtmlUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.wlh.learn.webmagic.pipeline.DBPipeline;
import cn.wlh.learn.webmagic.utils.ArrayUtils;
import cn.wlh.learn.webmagic.utils.SelectableUtils;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import java.sql.SQLException;
import java.util.List;

/**
 * webMagic获取cookie登陆
 * https://blog.csdn.net/weixin_42244754/article/details/81540678
 * ，账户过期了，没法用了。爬出来了，但是没法用了。
 * Selenium
 */
public class PiaochongSelenium extends AbstractDiscuzSelenium{
    private final static Log logger = LogFactory.get();
    public static final String URL_LIST = "https://www.snnna.com/forum.php?mod=forumdisplay&fid=53&page=";


    public static final String URL_POST_REGEX = "https://www.snnna.com/forum.php\\?.*";

    public PiaochongSelenium() {
        super("https://www.snnna.com/forum.php?mod=forumdisplay&fid=53&page=1");
    }


    @Override
    public void viewthread(Page page) {
        // 抓取规则
        Selectable url = page.getUrl();
//        logger.warn("url=[{}]", url);


        //列表页
        Html html = page.getHtml();


        // //*[@id="aimg_7996"]
        logger.debug("详情页面内容=[{}]", html);
        // 获得图片.没有就不记录.
        List<String> images = html.xpath("//ignore_js_op").links().all();
//            boolean empty = CollectionUtil.isEmpty(images);

        // 联系电话。
        List<String> phone = SelectableUtils.xpath(html,"//div[@class='showhide']/div[@class='ck8-tips-yd']/strong","//div[@class='showhide']").all();

        // 价格
        String rmb = html.xpath("//td[@class='t_f']/strong[2]").get();
        // 服务
        String fuwu = html.xpath("//td[@class='t_f']/strong[1]").get();

        // 判断是否标准.
        boolean isBiaoZhun = ArrayUtils.orExe((list) -> {
            return CollectionUtil.isEmpty(list);
        }, images, phone);
        String body = null;
        // 不是标准获得所有内容.
        if(isBiaoZhun){
        }
        body = html.xpath("//td[@class='t_f']").get();


        // body都没有，就是垃圾数据。放弃他.
        if( StrUtil.isBlank(body)) return;
        // 放入内容存储.

        page.putField("lianxifs",CollectionUtil.join(phone,"="));
        page.putField("rmb", rmb);
        page.putField("fuwu",fuwu);
        JSONArray field = JSONUtil.parseArray(images);
        String strImages = field.toString();
        page.putField("images", strImages);
        page.putField("body",body);

//            page.putField("title", page.getHtml().xpath("//div[@class='articalTitle']/h2"));
//            page.putField("content", page.getHtml().xpath("//div[@id='articlebody']//div[@class='articalContent']"));
//            page.putField("date",
//                    page.getHtml().xpath("//div[@id='articlebody']//span[@class='time SG_txtc']").regex("\\((.*)\\)"));
    }


    public static int PAGE = 11;
    public static void main(String[] args) throws SQLException {
        logger.warn("1111111111");
        PiaochongSelenium dome = new PiaochongSelenium();

        // 插入到。sheji_caiji_piaochong 表中.
        DBPipeline dBPipeline = new DBPipeline("sheji_caiji_piaochong");
        Spider.create(dome).addUrl(URL_LIST+PAGE)
                .addPipeline(dBPipeline)
                .thread(1).start();
    }
}